home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 6.5 KB | 199 lines |
- import java.awt.*;
- import java.net.URL;
-
- /**
- * This is a simple Macintosh-like lightweight progress bar bean.
- * This version does not support a disabled or background look,
- * nor does it support different Appearance Manager settings.
- * Please note, this is a simple example and was not written
- * to use a different image, nor was it written with extensibility
- * in mind.
- * @author Levi Brown
- * @author Apple Worldwide Developer Technical Support
- * @author Apple Computer Inc.
- * @version 1.0 10/29/1998
- */
- public class ProgressBar extends BufferedDrawer
- {
- /**
- * Creates a new ProgressBar with zero percent.
- */
- public ProgressBar()
- {
- //Initialize our data members
- percent = 0;
- progressImage = Util.loadImage(progressImagePath, this);
- }
-
- /**
- * Sets the percentage complete this progress bar will indicate.
- * @see #getPercent
- */
- public void setPercent(double percent)
- {
- if (percent < 0 || percent > 1)
- throw new IllegalArgumentException("" + percent + " needs to be greater than zero and less than or equal to one.");
-
- this.percent = percent;
- repaint();
- }
-
- /**
- * Gets the percentage complete this progress bar is currently indicating.
- * @see #setPercent
- */
- public double getPercent()
- {
- return percent;
- }
-
- /**
- * Returns the preferred size of this component.
- * Overriden here to constrain height.
- * @see java.awt.Component#getMinimumSize
- * @see LayoutManager
- */
- public Dimension getPreferredSize()
- {
- Dimension s = getSize();
- s.height = BAR_HEIGHT;
-
- return s;
- }
-
- /**
- * Renders the progress bar in an offscreen buffer.
- * Renders the bar to indicate the current percent complete.
- * @see #paint
- * @see #setPercent
- * @see #getPercent
- */
- protected void draw()
- {
- //Make sure we give the super a chance to do what it needs to.
- super.draw();
-
- //Get the size of the ofscreen image.
- int imageWidth = workingImage.getWidth(this);
- int imageHeight = workingImage.getHeight(this);
- //Determine the location of the percent to indicate
- int progressAreaWidth = imageWidth - 4;
- int progressLocation = (int)((percent * progressAreaWidth) + 2.5);
-
- //Erase the contents.
- workingGraphics.setColor(gray1);
- workingGraphics.fillRect(0, 0, imageWidth, imageHeight);
-
- //Draw the progress indicator
-
- //If there is not enough room to show the head and tail, just draw a flat color.
- if (progressLocation < 9)
- {
- //Draw the head (for the rightmost edge)
- workingGraphics.drawImage(progressImage, progressLocation - 4, 2, (progressLocation - 4) + 6, 13,
- 2, 0, 8, 10, this);
- //Fill the area with a flat color, over the left part of the head (to obfuscate the three dimensional look).
- workingGraphics.setColor(colorE);
- workingGraphics.fillRect(2, 2, progressLocation - 3, imageHeight - 4);
- }
- //There is enough room to draw the three dimensional look, so go ahead.
- else
- {
- int fillStart = 4;
- int fillEnd = progressLocation - 4;
-
- //Draw the fill
- workingGraphics.setColor(colorA);
- workingGraphics.drawLine(fillStart, 6, fillEnd, 6);
-
- workingGraphics.setColor(colorB);
- workingGraphics.drawLine(fillStart, 5, fillEnd, 5);
- workingGraphics.drawLine(fillStart, 7, fillEnd, 7);
-
- workingGraphics.setColor(colorC);
- workingGraphics.drawLine(fillStart, 4, fillEnd, 4);
- workingGraphics.drawLine(fillStart, 8, fillEnd, 8);
-
- workingGraphics.setColor(colorD);
- workingGraphics.drawLine(fillStart, 3, fillEnd, 3);
- workingGraphics.drawLine(fillStart, 9, fillEnd, 9);
-
- workingGraphics.setColor(colorE);
- workingGraphics.drawLine(fillStart, 2, fillEnd, 2);
- workingGraphics.drawLine(fillStart, 10, fillEnd, 10);
-
- workingGraphics.setColor(colorF);
- workingGraphics.drawLine(fillStart, 11, fillEnd, 11);
-
- //Draw the tail
- workingGraphics.drawImage(progressImage, 2, 2, 4, 13,
- 0, 0, 2, 10, this);
-
- //Draw the head
- workingGraphics.drawImage(progressImage, fillEnd, 2, fillEnd + 6 /*width of progress bar image*/, 13,
- 2, 0, 8, 10, this);
- }
-
- //Draw the border
- workingGraphics.setColor(getBackground());
- workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 1, imageHeight - 1);
- workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 2);
-
- workingGraphics.setColor(gray0);
- workingGraphics.drawLine(0, imageHeight - 1, 0, imageHeight - 1);
- workingGraphics.drawLine(imageWidth - 1, 0, imageWidth - 1, 0);
-
- workingGraphics.setColor(gray2);
- workingGraphics.drawLine(0, 0, 0, imageHeight - 2);
- workingGraphics.drawLine(1, 0, imageWidth - 2, 0);
-
- workingGraphics.setColor(white);
- workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 1);
- workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 2, imageHeight - 1);
-
- workingGraphics.setColor(black);
- workingGraphics.drawRect(1, 1, imageWidth - 3, imageHeight - 3);
-
- }
-
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by setBounds(int, int, int, int).
- * Overriden here to constrain height.
- */
- public void reshape(int x, int y, int width, int height)
- {
- //Make sure to call the super's reshape method and not the
- //super's setBounds method, or we end up in an infitite loop.
- super.reshape(x, y, width, BAR_HEIGHT);
- }
-
- //Hard coded colors to suppor the default Appearance Manager progress bar colors.
- protected static final Color white = Color.white;
- protected static final Color gray0 = new Color(222, 222, 222);
- protected static final Color gray1 = new Color(189, 189, 189);
- protected static final Color gray2 = new Color(173, 173, 173);
- protected static final Color gray3 = new Color(140, 140, 140);
- protected static final Color gray4 = new Color(82, 82, 82);
- protected static final Color black = Color.black;
-
- protected static final Color colorA = new Color(239, 239, 239);
- protected static final Color colorB = new Color(206, 206, 255);
- protected static final Color colorC = new Color(156, 156, 255);
- protected static final Color colorD = new Color(99, 99, 206);
- protected static final Color colorE = new Color(49, 49, 156);
- protected static final Color colorF = new Color(0, 0, 82);
-
- //Hard coded height for the progress bar.
- protected static final int BAR_HEIGHT = 14;
- //Relative location for the progress bar image file
- protected static final String progressImagePath = "images/progressbar.gif";
-
- /**
- * The current percentage the progress bar will display.
- */
- protected double percent;
- //The loaded image representing the head and tail of the three dimensional progress bar.
- protected Image progressImage;
- }
-